package com.heima.article.listener;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.wemedia.pojos.WmNews;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
public class ArticleListener {

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @KafkaListener(topics = "weNews-topic")
    public void receiverWmNewsData(String message){
        if (!StringUtils.isEmpty(message)){
            WmNews wmNews = JSONObject.parseObject(message, WmNews.class);
            Long articleId = wmNews.getArticleId();
            Short enable = wmNews.getEnable();
            ApArticleConfig apArticleConfig = apArticleConfigMapper.selectOne(new LambdaQueryWrapper<ApArticleConfig>()
                    .eq(ApArticleConfig::getArticleId, articleId));
            if (1==enable){
                apArticleConfig.setIsDown(false);
            }
            if (0==enable){
                apArticleConfig.setIsDown(true);
            }
            apArticleConfigMapper.updateById(apArticleConfig);
        }
    }
}
